$.namespace('act.taskTodoList');

act.taskTodoList = function() {

	return {
		init : function() {
			this.initGrid();
			this.initComponents();
		},
		initComponents : function(){
			
		},
		initGrid : function() {
			$('#grid').datagrid({
				url : ctxRest + '/act/task/list-todo',
				contentType : 'application/json;charset=utf-8',
				fit : true,
				fitColumns : true,
				rownumbers : true,
				nowrap : true,
				//pageList : [ 10, 15, 20, 30 ],
				//pageSize : 20,
				//pagination : true,
				striped : true,
				remoteSort : false,
				singleSelect : true,
				border : false,
				idField : 'id',
				columns : [ [ {
					title : '标题',
					field : 'vars',
					width : 120,
					formatter : function(value, row, index){ //console.log(row);
						return value.map.title;
					}
				}, {
					title : '流程名称',
					field : 'procDefName',
					width : 80
				}, {
					title : '当前环节',
					field : 'taskName',
					width : 80
				}, {
					title : '流程版本',
					field : 'procDefVersion',
					width : 60
				}, {
					title : '创建时间',
					field : 'taskCreateDate',
					width : 80
				}, /*{
					title : '发起人',
					field : '1',
					width : 60
				}, {
					title : '发起时间',
					field : '2',
					width : 60
				}, */{
					title : '操作',
					field : 'option',
					width : 120,
					formatter : function(value, row, index){
						var opts = '';
						//console.log(row);
						if (!row.assignee) {
							opts += '<a href="javascript:void(0);" onclick="act.taskTodoList.claim(' + "'" + row.taskId + "'" + ')">[ 签收 ]</a>';
						} else {
							opts += '<a href="javascript:void(0);" onclick="act.taskTodoList.processTask(' + "'" + row.taskDefKey + "','" + row.procDefId + "','" + row.procInsId + "','" + row.taskId + "','" + row.status + "'" + ')">[ 处理 ]</a>';
						}
						if (!row.executionId){
							//opts += '<a href="${ctx}/act/task/deleteTask?taskId=${task.id}&reason=" onclick="">[ 删除 ]</a>';
						}
						opts += '<a target="_blank" href="' + ctxRest + '/act/task/trace/photo/' + row.procDefId + '/' + row.executionId +'">[ 跟踪 ]</a>';
						return opts;
					}
				} ] ]
			});
		},
		processTask : function(taskDefKey, procDefId, procInsId, taskId, status) {
			$.ajax({
				type : 'POST',
				url : ctxRest + '/act/task/form',
				contentType : 'application/json;charset=utf-8',
				data : JSON.stringify({
					procDefId : procDefId,	//	流程定义ID
					procInsId : procInsId,	//	流程实例ID
					taskDefKey: taskDefKey,	//	任务标识 KEY
					taskId	: taskId,
					status	: status
				}),
				beforeSend : function(){
					progressLoad();
				},
				success : function(result){
					//console.log(result);
					//alert(result.url);
					window.location.href = (ctx + result.url);
				}
			});
		},
		claim : function(taskId) {
			$.messager.confirm('提示', '您确定要签收该任务?', function(flag){
				if (flag) {
					$.ajax({
						type : 'POST',
						url : ctxRest + '/act/task/claim',
						contentType : 'application/json;charset=utf-8',
						data : JSON.stringify({
							taskId : taskId
						}),
						beforeSend : function(){
							progressLoad();
						},
						success : function(result){
							progressClose();
							if (result && result.errcode == 0) {
								//$.messager.alert('提示', '签收成功！', 'info');
								$('#grid').datagrid('reload');
							}
						}
					})
				}
			});
		}
		
	};
}();

$(function() {
	act.taskTodoList.init();
})